package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.TravelComment;
import cn.wolfcode.wolf2w.query.TravelCommentQueryObject;
import cn.wolfcode.wolf2w.redis.service.IRedisService;
import cn.wolfcode.wolf2w.repository.TravelCommentRepository;
import cn.wolfcode.wolf2w.service.IDestinationService;
import cn.wolfcode.wolf2w.service.IRegionService;
import cn.wolfcode.wolf2w.service.ITravelCommentService;
import cn.wolfcode.wolf2w.util.DBHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class TravelCommentServiceImpl implements ITravelCommentService {

    @Autowired
    private TravelCommentRepository travelCommentRepository;
    @Autowired
    private IRedisService redisService;
    @Autowired
    private IRegionService regionService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private MongoTemplate template;

    @Override
    public void insert(TravelComment travelComment) {
        travelComment.setId(null);
        //还需要去维护第二层的字段数据
        String refId = travelComment.getRefComment().getId();
        if(StringUtils.hasLength(refId)){
            //获取第二层的评论
            TravelComment travelComment1 = this.selectOne(refId);
            travelComment.setRefComment(travelComment1);
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE);//评论的评论
        }else {
            travelComment.setType(TravelComment.TRAVLE_COMMENT_TYPE_COMMENT);//普通的评论
        }
        travelCommentRepository.insert(travelComment);

    }

    @Override
    public void delete(String id) {
        travelCommentRepository.deleteById(id);
    }

    @Override
    public void update(TravelComment travelComment) {
        travelCommentRepository.save(travelComment);
    }

    @Override
    public TravelComment selectOne(String id) {
        TravelComment travelComment = travelCommentRepository.findById(id).orElse(null);
        //TravelComment travelComment = byId.get();
        return travelComment;
    }

    @Override
    public List<TravelComment> selectAll() {
        List<TravelComment> all = travelCommentRepository.findAll();
        return all;
    }

    //@Override
    //public List<TravelComment> getDestByRegionId(String rid) {
    //    //1、通过区域id查询区域对象
    //    Region region = regionService.selectOne(rid);
    //    //2、从区域对象上获取目的地id的集合
    //    List<String> refIds = region.getRefIds();
    //    //3、将目的地id的集合转换成目的地名称的集合
    //    List<TravelComment> travelComments = travelCommentRepository.findByIdIn(refIds);
    //    return travelComments;
    //}

    @Override
    public Page<TravelComment> query(TravelCommentQueryObject q) {
        //1、创建查询对象
        Query query=new Query();

        //拼接攻略id的条件
        if(StringUtils.hasLength(q.getStrategyId())){
            query.addCriteria(Criteria.where("strategyId").is(q.getStrategyId()));
        }
        //2、判断keyword是否为空，若不为空，则进行条件的拼接
        if(StringUtils.hasLength(q.getKeyword())){
            query.addCriteria(Criteria.where("name").regex(q.getKeyword()));//进行拼接高级查询条件时需要使用regex
        }
        //判断父亲id长度是否为空，若为空，拼接成null
        if(!StringUtils.hasLength(q.getParentId())){
            query.addCriteria(Criteria.where("parentId").is(null));
        }else{
            query.addCriteria(Criteria.where("parentId").is(q.getParentId()));
        }
        //3、添加分页语句
        Pageable pageable = PageRequest.of(q.getCurrentPage()-1, q.getPageSize(), Sort.Direction.DESC, "_id");
        Page<TravelComment> page = DBHelper.query(q,query, TravelComment.class, template, pageable);
        return page;
    }

    //通过游记id查询对应的评论内容
    @Override
    public List<TravelComment> queryComments(String travelId) {
        //通过travelId查询出用户评论信息的对象
        List<TravelComment> travelComments= travelCommentRepository.queryByTravelId(travelId);
        List<TravelComment> list=new ArrayList<>();
        //判断是否有用户评论的嵌套
        for(TravelComment travelComment:travelComments){
            if(travelComment.getRefComment()!=null){
                list.add(travelComment);
            }else{
                list.add(travelComment);
            }
        }
        return list;
    }


}
